//地址栏id
function getURL(name) {
  const url = location.search;
  console.log(url);
  var urlSplit = url.substr(1);
  console.log(urlSplit);
  var arr = urlSplit.split("&");
  console.log(arr);
  var obj = {};
  for (var i = 0; i < arr.length; i++) {
    var newarr = arr[i].split("=");
    console.log(newarr);
    obj[newarr[0]] = newarr[1];
  }
  return obj[name];
}
const id = getURL("id");
console.log(id);
const id2 = $.cookie("userid");

//ajax
$(function () {
  $.ajax({
    url: "../server/fenye.php",
    type: "post",
    data: {
      id: id,
    },
  }).then((res) => {
    let result = JSON.parse(res);
    let dateReult = result.data[0];
    console.log(dateReult.good_name);

    let str = `
        <img id="zoom1" alt="${dateReult.good_name}" class="cloudzoom J_pro_bigImg" src="${dateReult.detail_img_1200}">`;

    $(".pro-bigImg").append(str);
    let listr = `
   <li class="i"><img class="cloudzoom-gallery loading" src="${dateReult.detail_img_220_2}" style="display: inline;"></li>
   <li class="i"><img o class="cloudzoom-gallery loading" src="${dateReult.detail_img_220_3}"
        style="display: inline;"></li>
   <li class="i"><img src="${dateReult.detail_img_220_4}" style="display: inline;"></li>
   <li class="i"><img src="${dateReult.detail_img_220_5}" style="display: inline;"></li>
   <li class="i hide"><img src="${dateReult.detail_img_220_2}" class="cloudzoom-gallery loading"></li>
   <li class="i hide"><img src="${dateReult.detail_img_220_2}" class="cloudzoom-gallery loading"></li>`;

    $(".silde_img").append(listr);

    //右侧标题 价格数据
    $(".goods-name").html(`${dateReult.good_name}`);

    $(".goods-sn").html(`款号:${dateReult.view}`);
    $(".goods-price").html(
      `￥${dateReult.price}<span class="mktprice">￥${dateReult.old_price}</span>`
    );

    $(".color_code").html(
      `
        <img title="${dateReult.size1}" src="${dateReult.legt_img}" class="loading">`
    );
  });
});

//加入购物车ajax

$(function () {
  $(".btn-addCart").click(function () {
    if (!id2) {
      toast("请先登录");
    } else {
      $.ajax({
        url: "../server/addCart.php",
        type: "post",
        data: {
          userid: id2,
          goodid: id
        },
        success(res) {
          console.log(res);
          let result = JSON.parse(res);
          console.log(result);
          if (result.code == 1) {
            toast("加入成功");
          } else {
            toast("商品已存在");
          }
        },
      });
    }
  });
});

$(function () {
  changeLogin();
  selfSearch();
});


//商品类别
$(function(){
    $('.color_list').on('click','li',function(){
      $(this).toggleClass('current').siblings().removeClass('current')
    })
})

//尺码类别
$(function(){
  $(".size_list").on('click','li',function(){
    $(this).toggleClass('current').siblings().removeClass('current')
})
})
